<?php
class ControllerAccountSpassword extends Controller {
	private $error = array();
	     
  	public function index() {	
    	if (!$this->customer->isLogged()) {
      		$this->session->data['redirect'] = $this->url->link('account/spassword', '', 'SSL');

      		$this->redirect($this->url->link('account/login', '', 'SSL'));
    	}

		$this->language->load('account/spassword');

    	$this->document->setTitle($this->language->get('heading_title'));
			  
    	
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) 
    	{
			$this->load->model('account/customer');

			$this->model_account_customer->editsPassword($this->customer->getEmail(), $this->request->post['spassword']);
			
      		$this->session->data['success'] = $this->language->get('text_success');
	  
	  		$this->redirect($this->url->link('account/account', '', 'SSL'));
    	}
    

      	$this->data['breadcrumbs'] = array();

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),       	
        	'separator' => false
      	); 

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_account'),
			'href'      => $this->url->link('account/account', '', 'SSL'),
        	'separator' => $this->language->get('text_separator')
      	);
		
      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('account/spassword', '', 'SSL'),
        	'separator' => $this->language->get('text_separator')
      	);
			
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	
    	$this->data['entry_oldspassword'] = $this->language->get('entry_oldspassword');
    	$this->data['entry_spassword'] = $this->language->get('entry_spassword');
    	$this->data['entry_sconfirm'] = $this->language->get('entry_sconfirm');

    	$this->data['button_continue'] = $this->language->get('button_continue');
    	$this->data['button_back'] = $this->language->get('button_back');
    	        
        $this->data['text_email'] = $this->language->get('text_email');
        $this->data['text_spassword'] = $this->language->get('text_spassword');
        $this->data['text_logged'] = sprintf($this->customer->getEmail());
            
		
  		if (isset($this->error['oldspassword'])) { 
			$this->data['error_oldspassword'] = $this->error['oldspassword'];
		} else {
			$this->data['error_oldspassword'] = '';
		}
  		if (isset($this->error['spassword'])) { 
			$this->data['error_spassword'] = $this->error['spassword'];
		} else {
			$this->data['error_spassword'] = '';
		}

		if (isset($this->error['sconfirm'])) { 
			$this->data['error_sconfirm'] = $this->error['sconfirm'];
		} else {
			$this->data['error_sconfirm'] = '';
		}
	
    	$this->data['action'] = $this->url->link('account/spassword', '', 'SSL');
		
		
  		if (isset($this->request->post['oldspassword'])) {
    		$this->data['oldspassword'] = $this->request->post['oldspassword'];
		} else {
			$this->data['oldspassword'] = '';
		}
  		if (isset($this->request->post['spassword'])) {
    		$this->data['spassword'] = $this->request->post['spassword'];
		} else {
			$this->data['spassword'] = '';
		}
		if (isset($this->request->post['sconfirm'])) {
    		$this->data['sconfirm'] = $this->request->post['sconfirm'];
		} else {
			$this->data['sconfirm'] = '';
		}
  	if (isset($this->request->post['changespassword'])) {
    		$this->data['changespassword'] = $this->request->post['changespassword'];
		} else {
			$this->data['changespassword'] = '';
		}
    	$this->data['back'] = $this->url->link('account/account', '', 'SSL');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/spassword.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/spassword.tpl';
		} else {
			$this->template = 'default/template/account/spassword.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);
						
		$this->response->setOutput($this->render());			
  	}
        
  	private function validate() {
  		$this->load->model('account/customer');
    		if ((utf8_strlen($this->request->post['oldspassword']) >= 6) && (utf8_strlen($this->request->post['oldspassword']) < 30)) {
      			$oldspwd = 	$this->request->post['oldspassword'];
      			$customerinfo = $this->model_account_customer->getCustomerByEmail($this->customer->getEmail());
      			if($customerinfo['spassword'] != md5($oldspwd)){
      				$this->error['oldspassword'] = $this->language->get('error_oldspwd');
      			}
	    	}else{
	    		$this->error['oldspassword'] = $this->language->get('error_oldspassword');
	    	}
	
	  		if ((utf8_strlen($this->request->post['spassword']) < 6) || (utf8_strlen($this->request->post['spassword']) > 30)) {
	      		$this->error['spassword'] = $this->language->get('error_spassword');
	    	}
	    	if ($this->request->post['sconfirm'] != $this->request->post['sconfirm']) {
	      		$this->error['sconfirm'] = $this->language->get('error_sconfirm');
	    	}  
		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}

}
?>
